package com.pig4cloud.ila.platform.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.ila.client.api.dto.ArticleDTO;
import com.pig4cloud.ila.client.api.entity.Article;
import com.pig4cloud.ila.platform.service.PlatformArticleService;
import com.pig4cloud.pig.common.core.constant.QueryConstants;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author KonBAI
 * @version 1.0
 * Create by 2020/10/23 01:03
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/article")
@Api(value = "article", tags = "平台方文章管理")
public class PlatformArticleController {

    private final PlatformArticleService platformArticleService;

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param articleDTO 文章管理表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @SysLog("分页查询文章管理表")
    @GetMapping("/page")
    public R getArticleInfoPage(Page page, ArticleDTO articleDTO) {
        return R.ok(platformArticleService.findPage(page, articleDTO));
    }

    /**
     * 通过id查询文章管理表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @SysLog("查询文章管理表")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {
        return R.ok(platformArticleService.getById(id));
    }

    /**
     * 修改文章管理表
     *
     * @param article 文章管理表
     * @return R
     */
    @ApiOperation(value = "修改文章管理表", notes = "修改文章管理表")
    @SysLog("修改文章管理表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('client_article_edit')")
    public R updateById(@RequestBody Article article) {
        return R.ok(platformArticleService.updateById(article));
    }

    /**
     * 详情
     *
     * @param id 文章ID
     * @return
     */
    @ApiOperation(value = "文章详情", notes = "文章详情")
    @SysLog("文章详情")
    @GetMapping("/getContent/{id}")
    public R getArticleContent(@PathVariable("id") String id) {
        Article article = platformArticleService.getOne(Wrappers.lambdaQuery(new Article())
                .select(Article::getContent)
                .eq(Article::getId, id)
                .last(QueryConstants.SELECT_LiMIT_ONE));
        if (ObjectUtil.isEmpty(article)) {
            return R.failed();
        }
        return R.ok(article.getContent());
    }

}
